/*
 * Copyright (C) 2004-2007  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoCMSOverrides.h"
#include "FdoCMSXmlGlobals.h"
#include <CMS/Override/FdoCMSOvRasterDefinition.h>
#include "../Provider/FdoCMSGlobals.h"

FdoCMSOvRasterDefinition::FdoCMSOvRasterDefinition(void) :
    m_pXmlContentHandler(NULL),
    m_formatType(FdoCMSXmlGlobals::g_CMSImageFormatPng)
    //m_formatDesc(FdoCMSGlobals::RasterMIMEFormat_PNG),
    //m_hasFormat(false),
    //m_hasFormatType(false),
{
}

FdoCMSOvRasterDefinition::~FdoCMSOvRasterDefinition(void)
{
    FDO_SAFE_RELEASE(m_pXmlContentHandler);
}

void FdoCMSOvRasterDefinition::Dispose()
{
	delete this;
}

FdoCMSOvRasterDefinition* FdoCMSOvRasterDefinition::Create()
{
	return new FdoCMSOvRasterDefinition();
}

FdoString* FdoCMSOvRasterDefinition::GetImageFormat(void) 
{
    //if (!m_hasFormatType && m_hasFormat) // value is from CMS 3.2.2 version configuration
    //{
    //    // set <FormatType> interpreted from <Format>
    //    m_formatDesc = this->AbbrToMIMEFormatType(m_formatType);
    //}

    // m_formatDesc is valid for CMS 3.3    
    return m_formatType;
}

// Input value would like: "image/png; PhotometricInterpretation=PaletteColor"
// or "image/png"
void FdoCMSOvRasterDefinition::SetImageFormat(FdoString* value)
{
    VALIDATE_ARGUMENT(value);

    //// Set the CMS 3.3 tag: <FormatType>
    //_SetFullFormatType(value);

    //// Maintain the CMS 3.2.2 tag: <Format>PNG</Format> only allows 4 types: PNG, TIF, GIF and JPG.
    //FdoStringP format = this->MIMEFormatTypeToAbbr(value);
    //_SetFormatAbbr(format);
    _SetFormatAbbr(value);
}

void FdoCMSOvRasterDefinition::InitFromXml(FdoXmlSaxContext* pContext, FdoXmlAttributeCollection* attrs)
{
    return BaseType::InitFromXml(pContext, attrs);
}

// Handle the start of a sub-element
FdoXmlSaxHandler* FdoCMSOvRasterDefinition::XmlStartElement(
   FdoXmlSaxContext* context, 
   FdoString* uri, 
   FdoString* name, 
   FdoString* qname, 
   FdoXmlAttributeCollection* atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try 
	{
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);
		
		pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
		if (pRet == NULL) {
			//if (FdoCommonStringUtil::StringCompareNoCase(name, FdoCMSXmlGlobals::g_CMSLayerDefinition) == 0) {
			//	FdoCMSOvLayerDefinitionP pNewObj = FdoCMSOvLayerDefinition::Create();
			//	pNewObj->InitFromXml(context, atts);
			//	m_layers->Add(pNewObj);
			//	pRet = pNewObj;
			//}
   //         else 
            if (FdoCommonStringUtil::StringCompareNoCase(name, FdoCMSXmlGlobals::g_CMSImageFormat) == 0 ||
                     FdoCommonStringUtil::StringCompareNoCase(name, FdoCMSXmlGlobals::g_CMSImageFormatType) == 0) 
            {
                m_pXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = m_pXmlContentHandler;
            }
		}
	}
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return pRet;
}

// Handle the end of a sub-element
FdoBoolean FdoCMSOvRasterDefinition::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        BaseType::XmlEndElement(context, uri, name, qname);

        if (FdoCommonOSUtil::wcsicmp(name, FdoCMSXmlGlobals::g_CMSImageFormat) == 0) {
            _SetFormatAbbr(m_pXmlContentHandler->GetString());
        }
        //else if (FdoCommonOSUtil::wcsicmp(name, FdoCMSXmlGlobals::g_CMSImageFormatType) == 0) {
        //    _SetFullFormatType(m_pXmlContentHandler->GetString());
        //}

        FDO_SAFE_RELEASE(m_pXmlContentHandler);
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return false;
}

// Write this element to XML.
void FdoCMSOvRasterDefinition::_writeXml( FdoXmlWriter* xmlWriter, const FdoXmlFlags* flags )
{
    VALIDATE_ARGUMENT(xmlWriter);
    VALIDATE_ARGUMENT(flags);
	
	xmlWriter->WriteStartElement(FdoCMSXmlGlobals::g_CMSRasterDefinition);
	BaseType::_writeXml(xmlWriter, flags);

    xmlWriter->WriteStartElement(FdoCMSXmlGlobals::g_CMSImageFormat);
    xmlWriter->WriteCharacters(_GetFormatAbbr());
    xmlWriter->WriteEndElement();

    //xmlWriter->WriteStartElement(FdoCMSXmlGlobals::g_CMSImageFormatType);
    //xmlWriter->WriteCharacters(GetImageFormat());
    //xmlWriter->WriteEndElement();

	xmlWriter->WriteEndElement();
}

// This one is for CMS provider 3.3 tag: <FormatType>
// <FormatType>image/png;PhotometricInterpretation=PaletteColor</FormatType>
// or
// <FormatType>image/png</FormatType>
//void FdoCMSOvRasterDefinition::_SetFullFormatType(FdoString *value)
//{
//    VALIDATE_ARGUMENT(value);
//
//    bool bMatch = false;
//    FdoStringP format(value);
//
//    // Format contains extra parameter
//    if (format.Contains(FdoCMSGlobals::RasterFormatSemicolon))
//        format = format.Left(FdoCMSGlobals::RasterFormatSemicolon);
//
//    for (int i = 0; FdoCMSXmlGlobals::g_MIMEImageFormats[i] != NULL; i++)
//    {
//        if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)format, FdoCMSXmlGlobals::g_MIMEImageFormats[i]) == 0)
//        {
//            bMatch = true;
//            m_formatDesc = value;
//            break;
//        }
//    }
//
//    if (!bMatch)
//        throw FdoSchemaException::Create (NlsMsgGet (FDOCMS_INVALID_XMLSCHEMA_RASTERFORMATTYPE, "'%1$ls' is not a valid XML Raster Format Type.", value));
//
//    // <FormatType> is from XML file
//    //m_hasFormatType = true;
//}

// This one for the CMS 3.2.2 tag: 
// <Format>PNG</Format> only allows 4 types: PNG, TIF, GIF and JPG.
void FdoCMSOvRasterDefinition::_SetFormatAbbr(FdoString* value)
{
    VALIDATE_ARGUMENT(value);

    if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)value, FdoCMSXmlGlobals::g_CMSImageFormatPng) == 0) {
        m_formatType = FdoCMSXmlGlobals::g_CMSImageFormatPng;
    }
    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)value, FdoCMSXmlGlobals::g_CMSImageFormatTif) == 0) {
        m_formatType = FdoCMSXmlGlobals::g_CMSImageFormatTif;
    }
    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)value, FdoCMSXmlGlobals::g_CMSImageFormatJpg) == 0) {
        m_formatType = FdoCMSXmlGlobals::g_CMSImageFormatJpg;
    }
    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)value, FdoCMSXmlGlobals::g_CMSImageFormatJpeg) == 0) {
        m_formatType = FdoCMSXmlGlobals::g_CMSImageFormatJpeg;
    }
    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)value, FdoCMSXmlGlobals::g_CMSImageFormatGif) == 0) {
        m_formatType = FdoCMSXmlGlobals::g_CMSImageFormatGif;
    }
    else {
        throw FdoSchemaException::Create (NlsMsgGet (FDOCMS_INVALID_XMLSCHEMA_RASTERFORMATTYPE, "'%1$ls' is not a valid XML Raster Format Type.", value));
    }
    
    // <Format> is from XML file
    //m_hasFormat = true;
}

FdoStringP FdoCMSOvRasterDefinition::_GetFormatAbbr(void) const
{       
    return m_formatType;
}

FdoStringP FdoCMSOvRasterDefinition::GetQualifiedName()
{
    FdoStringP ret = this->GetName ();
    FdoPtr<FdoPhysicalElementMapping> parent = this->GetParent ();
    if (parent != NULL)
    {
        FdoStringP parentQName = parent->GetQualifiedName ();
        if (parentQName.GetLength () > 0)
        {
            ret = parentQName + L"." + ret;
        }
    }

    return ret;
}

//// Convert from CMS 3.3 format type to CMS 3.2.2 value
//FdoStringP FdoCMSOvRasterDefinition::MIMEFormatTypeToAbbr(FdoString* fullFormat)
//{
//    VALIDATE_ARGUMENT(fullFormat);
//
//    // Ignore the extra parameter
//    FdoStringP format(fullFormat);
//    if (format.Contains(FdoCMSGlobals::RasterFormatSemicolon))
//        format = format.Left(FdoCMSGlobals::RasterFormatSemicolon);
//
//    FdoStringP ret;
//    if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)format, FdoCMSGlobals::RasterMIMEFormat_PNG) == 0) {
//        ret = FdoCMSXmlGlobals::g_CMSImageFormatPng;    // convert from "image/png" to "PNG"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)format, FdoCMSGlobals::RasterMIMEFormat_TIFF) == 0) {
//        ret = FdoCMSXmlGlobals::g_CMSImageFormatTif;    // convert from "image/tiff" to "TIF"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)format, FdoCMSGlobals::RasterMIMEFormat_JPEG) == 0) {
//        ret = FdoCMSXmlGlobals::g_CMSImageFormatJpg;    // convert from "image/jpeg" to "JPG"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)format, FdoCMSGlobals::RasterMIMEFormat_GIF) == 0) {
//        ret = FdoCMSXmlGlobals::g_CMSImageFormatGif;    // convert from "image/gif" to "GIF"
//    }
//    else {
//        throw FdoSchemaException::Create (NlsMsgGet (FDOCMS_INVALID_XMLSCHEMA_RASTERFORMATTYPE, "'%1$ls' is not a valid XML Raster Format Type.", fullFormat));
//    }
//
//    return ret;
//}
//
//// Convert from CMS 3.3 format type to CMS 3.2.2 value
//FdoStringP FdoCMSOvRasterDefinition::AbbrToMIMEFormatType(FdoString* abbrFormat)
//{
//    VALIDATE_ARGUMENT(abbrFormat);
//
//    FdoStringP ret;
//    if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)abbrFormat, FdoCMSXmlGlobals::g_CMSImageFormatPng) == 0) {
//        ret = FdoCMSGlobals::RasterMIMEFormat_PNG;    // convert from "PNG" to "image/png"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)abbrFormat, FdoCMSXmlGlobals::g_CMSImageFormatTif) == 0) {
//        ret = FdoCMSGlobals::RasterMIMEFormat_TIFF;    // convert from "TIF" to "image/tiff"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)abbrFormat,FdoCMSXmlGlobals::g_CMSImageFormatJpg) == 0) {
//        ret = FdoCMSGlobals::RasterMIMEFormat_JPEG;    // convert from "JPG" to "image/jpeg"
//    }
//    else if (FdoCommonStringUtil::StringCompareNoCase((FdoString*)abbrFormat,FdoCMSXmlGlobals::g_CMSImageFormatGif) == 0) {
//        ret = FdoCMSGlobals::RasterMIMEFormat_GIF;    // convert from "GIF" to "image/gif"
//    }
//    else {
//        throw FdoSchemaException::Create (NlsMsgGet (FDOCMS_INVALID_XMLSCHEMA_RASTERFORMATTYPE, "'%1$ls' is not a valid XML Raster Format Type.", abbrFormat));
//    }
//
//    return ret;
//}
